package com.reflect.test;

import com.reflect.entity.Student;

import javax.swing.plaf.synth.SynthUI;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author wzm
 * @create 2023-04-06 10:31
 */


public class RefTest06 {
    public static void main(String[] args) throws Exception {
        //获取类的字节码文件
        Class clazz = Class.forName("com.reflect.entity.Student");
        
        //获得类的方法
        
        //1.获取类的public修改的方法  包含了父类继承的public方法
        Method[] methods = clazz.getMethods();
        for(Method method : methods){
            System.out.println("method = " + method);
        }

        System.out.println("-----------------------------------");

        //2.获取所有方法的集合 public+private 不包括从父类继承过来的方法
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for(Method method : declaredMethods){
            System.out.println("method = " + method);
        }

        System.out.println("-----------------------------------");
        //3.获取某个公开的方法
        //空参  sleep
        Method sleep = clazz.getMethod("sleep");
        System.out.println("sleep = " + sleep);

        //有参 eat
        Method eat = clazz.getMethod("eat", String.class);
        System.out.println("eat = " + eat);

        //创建对象
        Student student = new Student();
        System.out.println("-----------------------------------");
        //调用方法
        //空参
        Object invoke = sleep.invoke(student);
        System.out.println("invoke = " + invoke);

        System.out.println("-----------------------------------");

        //有参
        String pig = (String) eat.invoke(student, "猪脚米线");
        System.out.println("pig = " + pig);

        //私有方法
        Method sayHello = clazz.getDeclaredMethod("sayHello", String.class);
        //设置临时权限
        sayHello.setAccessible(true);

        //调用
        String ldh = (String) sayHello.invoke(student, "刘德华");
        System.out.println("ldh = " + ldh);


    }

}
